package com.project.study.farming.base.service.impl;

import com.project.study.farming.base.entity.dto.UserDto;
import com.project.study.farming.base.service.UserSelfService;
import com.project.study.farming.common.entity.po.UserPo;
import com.project.study.farming.common.utils.BeanMapper;
import com.project.study.farming.common.utils.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 个人中心实现类
 * @since 2020-05-11
 */
@Service
public class UserSelfServiceImpl implements UserSelfService {

    @Autowired
    private BeanMapper beanMapper;

    @Override
    public ModelAndView userCenterPage() {
        return CommonUtil.success("userCenter.html", null);
    }

    @Override
    public ModelAndView editUserCenter(UserDto user) {
        UserPo curUser = CommonUtil.getUser();
        UserPo userPo = new UserPo();
        String password = user.getPassword();
        BeanUtils.copyProperties(user, userPo);
        userPo.setLoginId(curUser.getLoginId());
        userPo.setRealPassword(password);
        userPo.setIsVip(curUser.getIsVip());
        userPo.setIsDelete(curUser.getIsDelete());
        userPo.setCreateBy(curUser.getCreateBy());
        userPo.setCreateTime(curUser.getCreateTime());
        userPo.setPassword(CommonUtil.encryptStr(password));
        userPo.setUpdateBy(curUser.getUsername());
        userPo.setUpdateTime(LocalDateTime.now());
        this.beanMapper.updateById(userPo);
        CommonUtil.saveDataToSession(userPo);
        return this.userCenterPage();
    }
}
